package com.ruoyi.notice.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.notice.config.RabbitConfig;
import com.ruoyi.notice.domain.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.notice.domain.carNotice;
import com.ruoyi.notice.service.IcarNoticeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 车联网通知公告Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/notice")
public class carNoticeController extends BaseController
{
    @Autowired
    private IcarNoticeService carNoticeService;
    @Autowired
    private RabbitTemplate rabbitTemplate;



    /**
     * 查询商家用户发布的活动
     */
    @RequiresPermissions("notice:notice:list")
    @GetMapping("getActivityByShopUserId")
    public AjaxResult getActivityByShopUserId(@RequestParam Long userId){
        System.out.println(userId);
        List<carNotice> list = carNoticeService.getActivityByShopUserId(userId);
        return AjaxResult.success(list);
    }

    /**
     * 商家发布活动
     */
    @RequiresPermissions("notice:notice:add")
    @RequestMapping("/addActivity")
    public AjaxResult addActivity(@RequestBody Map map){
//        System.out.println(map);
        carNoticeService.addActivity(map);
        return AjaxResult.success();
    }



    /**
     * 查询车联网通知公告列表
     */
    @RequiresPermissions("notice:notice:list")
    @GetMapping("/list")
    public TableDataInfo list(carNotice carNotice)
    {
        startPage();
        List<carNotice> list = carNoticeService.selectcarNoticeList(carNotice);
        return getDataTable(list);
    }

    /**
     * 导出车联网通知公告列表
     */
    @RequiresPermissions("notice:notice:export")
    @Log(title = "车联网通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, carNotice carNotice)
    {
        List<carNotice> list = carNoticeService.selectcarNoticeList(carNotice);
        ExcelUtil<carNotice> util = new ExcelUtil<carNotice>(carNotice.class);
        util.exportExcel(response, list, "车联网通知公告数据");
    }

    /**
     * 获取车联网通知公告详细信息
     */
    @RequiresPermissions("notice:notice:query")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        return success(carNoticeService.selectcarNoticeByNoticeId(noticeId));
    }

    /**
     * 新增车联网通知公告
     */
    @RequiresPermissions("notice:notice:add")
    @Log(title = "车联网通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody carNotice carNotice)
    {
        return toAjax(carNoticeService.insertcarNotice(carNotice));
    }

    /**
     * 修改车联网通知公告
     */
    @RequiresPermissions("notice:notice:edit")
    @Log(title = "车联网通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody carNotice carNotice)
    {
        return toAjax(carNoticeService.updatecarNotice(carNotice));
    }

    /**
     * 删除车联网通知公告
     */
    @RequiresPermissions("notice:notice:remove")
    @Log(title = "车联网通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(carNoticeService.deletecarNoticeByNoticeIds(noticeIds));
    }

    /**
     * 订单消息通知
     *传过来的消息要有消息标题（例如订单支付成功或者订单支付失败）、消息内容（自己拼接字符串即可）、uid（用户id）
     */
    @InnerAuth
    @PostMapping("/orderMessage")
    public AjaxResult orderMessage(@RequestBody Message message){
        //发送消息到交换机
        rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"order",message);
        return success();
    }

    /**
     * 救援消息通知
     * message要有消息标题（请求救援）、消息内容（自己拼接字符串即可）,uid（用户id）,hid(救援消息id)
     */
//    @InnerAuth
    @PostMapping("/rescueMessage")
    public AjaxResult rescueMessage(@RequestBody Message message){
        //发送消息到交换机
        rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"rescue",message);
        return success();
    }

    /**
     * 删除救援消息通知
     */
//    @InnerAuth
    @GetMapping("/deleteRescueMessage")
    public AjaxResult deleteRescueMessage(@RequestParam String hid){
        Message message = new Message();
        message.setHid(hid);
        //发送消息到交换机
        rabbitTemplate.convertAndSend(RabbitConfig.CAR_NOTICE_EXCHANGE,"deleteRescue",message);
        return success();
    }

    /**
     * 获取指定商家的救援消息
     */
    @GetMapping("getRescueMessageBySid")
    public AjaxResult getRescueMessageBySid(@RequestParam int sid){
        List<Message> list = carNoticeService.getRescueMessageBySid(sid);
        return success(list);
    }
}
